<link rel="stylesheet" href="/static/lib/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script src="/static/lib/ztree/jquery.ztree.core.js" charset="utf-8"></script>
<script src="/static/lib/ztree/jquery.ztree.excheck.js" charset="utf-8"></script>

<SCRIPT type="text/javascript">


    var url = '{{ .CommonIn.GetUrlByKey "admin/user/DoGrantSubmit" }}';
    var resId = {{.resId}};
    var myRoleList = {{.myRoleJson}};
    function subData (data){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: url,
            data:data,
            success: function (res) {
                layer.close(loadIndex);

                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        parent.CloseChild(true)
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(){
                layer.close(loadIndex);
            },
            complete:function (){

            }
        });
    }


    layui.use(['form', ], function () {
        let _$ = layui.$;
        let form = layui.form
        let val = {};
        for(let i=0;i<myRoleList.length;i++){
            val["role["+myRoleList[i]+"]"] = true
        }
        val.resId = resId

        form.val('submitForm', val)

        $("#cancelAdd").click(function(e){
            e.preventDefault();
            parent.CloseChild(false)
        })
        //监听提交
        $("#submitForm").click(function(e){
            e.preventDefault()
            let formData = form.val('submitForm');
            let roleList = []
            _$('input.role:checked').each(function(index,val){
                roleList.push(_$(val).val())
            })

            subData({
                resId:resId,
                roleList:roleList
            })
             
        })

    });

</SCRIPT>